package com.powergisol.gis.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.powergisol.gis.entity.CoordinateSystem;
import com.powergisol.gis.service.CoordinateSystemService;
import com.powergisol.gis.util.Result;
import com.powergisol.gis.vo.CoordinateSystemVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.type.CollectionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/coordinate")
@Api(description = "坐标转换公式管理", value = "坐标转换公式管理")
public class CoordinateSystemController {
    @Autowired
    private CoordinateSystemService systemService;

    /**
     * 根据ID查询坐标系转换公式
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID查询", notes = "根据ID查询")
    @ApiImplicitParam(dataType = "Integer", name = "id", value = "坐标系转换公式的ID", required = true, paramType = "query")
    @GetMapping("/selectById")
    public Result selectById(Integer id) {
        CoordinateSystem coordinateSystem = this.systemService.selectByPrimaryKey(id);
        return new Result().success(coordinateSystem);
    }

    /**
     * 新增坐标系转换公式
     *
     * @param system
     * @return
     */
    @PostMapping("/insertData")
    @ApiOperation(value = "新增坐标转换公式", notes = "新增坐标转换公式")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "coorName", value = "坐标系转换公式的名称", required = true, paramType = "body"),
            @ApiImplicitParam(dataType = "String", name = "coorChangeLon", value = "经度-坐标系转换公式", required = true, paramType = "body"),
            @ApiImplicitParam(dataType = "String", name = "coorChangeLat", value = "纬度-坐标系转换公式", required = true, paramType = "body")
    })
    public Result insertData(CoordinateSystem system) {
        if (this.systemService.insert(system)) {
            return new Result().success("新增成功");
        } else {
            return new Result().failure(500, "新增失败");
        }
    }

    /**
     * 修改坐标系转换公式
     *
     * @param system
     * @return
     */
    @PostMapping("/updateData")
    @ApiOperation(value = "修改坐标转换公式", notes = "修改坐标转换公式")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "coorName", value = "坐标系转换公式的名称", required = true, paramType = "body"),
            @ApiImplicitParam(dataType = "String", name = "coorChangeLon", value = "经度-坐标系转换公式", required = true, paramType = "body"),
            @ApiImplicitParam(dataType = "String", name = "coorChangeLat", value = "纬度-坐标系转换公式", required = true, paramType = "body")
    })
    public Result updateData(CoordinateSystem system) {
        if (this.systemService.updateByPrimaryKey(system)) {
            return new Result().success("修改成功");
        } else {
            return new Result().failure(500, "修改失败");
        }
    }

    /**
     * 根据ID删除坐标转换公式
     *
     * @param ids
     * @return
     */
    @PostMapping("/deleteData")
    @ApiOperation(value = "批量删除坐标转换公式", notes = "批量删除坐标转换公式")
    @ApiImplicitParam(dataType = "List", name = "ids", value = "ID集合", required = true, paramType = "query")
    public Result deleteData(@RequestParam("ids") String ids) {
        ObjectMapper mapper = new ObjectMapper();
        CollectionType collectionType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, Integer.class);
        List<Integer> IDS=null;
        try {
            IDS = mapper.readValue(ids, collectionType);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
        if (this.systemService.deleteByPrimaryKey(IDS)) {
            return new Result().success("删除成功");
        } else {
            return new Result().failure(500, "删除失败");
        }
    }

    /**
     * 分页查询所有的坐标转换公式
     *
     * @param systemVO
     * @return
     */
    @GetMapping("/selectAll")
    @ApiOperation(value = "分页查询坐标转换公式", notes = "分页查询坐标转换公式")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "page", value = "页数", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "limit", value = "条数", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "name", value = "坐标转换公式的名称" , paramType = "query")
    })
    public Result selectAll(CoordinateSystemVO systemVO) {
        Result result = new Result();
        PageHelper.startPage(systemVO.getPage(), systemVO.getLimit());
        PageInfo<CoordinateSystem> pageInfo = new PageInfo<>(this.systemService.selectAll(systemVO));
        result.setCount((int) pageInfo.getTotal());
        result.setData(pageInfo.getList());
        return result;
    }



}
